package com.kob.botruningsystem.utils;


import java.io.File;
import java.io.FileNotFoundException;
import java.util.Scanner;

public class Bot implements java.util.function.Supplier<Integer>{
     //请不要修改函数以外的信息，报ce会导致蛇没有输入直接失败
    public Integer nextMove(String input) {//函数会传来地图信息(0代表空，1代表墙(包括蛇身不包括蛇尾)，2代表食物，3代表敌方蛇头，4代表我方蛇头)
        String[] elements = input.substring(1, input.length() - 1).split(",");//去除首尾括号并分割
        int[][] map = new int[30][31];
        int ii=-1,jj=0;
        for(int i = 0;i < elements.length;i++,jj++){
            String el = elements[i].trim();
            if (el.startsWith("[")) {
                ii++;
                jj =0;
                el = el.substring(1,el.length());
            } else if (el.endsWith("]")) {
                el = el.substring(0,el.length()-1);
            }
            System.out.println(el);
            map[ii][jj] = Integer.parseInt(el);
        }

        int me_x = 1,me_y = 1;
        for(int i = 0; i < map.length;i++){
            for(int j =0; j < map[i].length;j++){
                if(map[i][j] == 4)
                {
                    me_x = i;
                    me_y = j;
                }
            }
        }
        int[] dx = {-1,1,0,0};
        int[] dy = {0,0,-1,1};
        for(int i = 0;4>i;i++){
            if(map[me_x + dx[i]][me_y+dy[i]] == 0)
            {
                if(map[me_x + dx[i] + dx[i]][me_y+dy[i] + dy[i]] == 0)
                {
                    if(map[me_x + dx[i] + dx[i] + dx[i]][me_y+dy[i] + dy[i] + dy[i]] == 0){
//                        System.out.println("return  "+i + " mex "+me_x+ " mey "+me_y);
//                        System.out.println("Map"+ Arrays.deepToString(map));
                        return i;
                    }

                }
            }
        }
        for(int i = 0;4>i;i++){
            if(map[me_x + dx[i]][me_y+dy[i]] == 0)
            {
                if(map[me_x + dx[i] + dx[i]][me_y+dy[i] + dy[i]] == 0)
                {
//                    System.out.println("return  "+i + " mex "+me_x+ " mey "+me_y);
//                    System.out.println("Map"+ Arrays.deepToString(map));
                    return i;
                }
            }
        }
        for(int i = 0;4>i;i++){
            if(map[me_x + dx[i]][me_y+dy[i]] == 0)
            {
//                System.out.println("return  "+i + " mex "+me_x+ " mey "+me_y);
                return i;
            }
        }
//        System.out.println("return  ");
        return 3;
    }

    @Override //不要修改这个函数，否则会变得不幸
    public Integer get() {
        File file = new File("input.txt");
        try {
            Scanner sc = new Scanner(file);
            return nextMove(sc.next());

        } catch (FileNotFoundException e) {
            throw new RuntimeException(e);
        }

    }
}
